<?php
/**
 * @since      File available since Release v1.1
 */
defined('Inbearshop') or exit('Access Invalid!');
define('MYSQL_RESULT_TYPE',1);
class publishControl extends BaseHomeControl{
	public function __construct(){
		parent::__construct();
        Language::read('indexs');
	}

	public function indexOp(){
		$this->step1Op();
	}
	
	public function step1Op(){			
		if(isset($_POST)&&!empty($_POST)){		
			$params = array();
			if(empty($_POST['auction_description'])){
				echo json_encode(array('state'=>'1','error'=>'描述信息不能为空'));
				exit;
			}
			$model = Model();
			$auction_id = intval($_POST['auction_id']);
			$auction_img = $model->table('auction_img')->where(array('auction_id'=>$auction_id))->select();
			if(empty($auction_img)){
				echo json_encode(array('state'=>'1','error'=>'图像不能为空'));
				exit;
			}			
			$params['auction_description'] = $_POST['auction_description'];
			
			$auction_img = $model->table('auction_img')->where(array('auction_id'=>$auction_id))->order('id asc')->find();
			$auction_video_url=$_POST["auction_video_url"];
			$params['auction_video']=$auction_video_url;
			$params['auction_img1'] = $auction_img['auction_img'];

			$res = $model->table('auction')->where(array('id'=>$auction_id))->update($params);
			if($res){
				echo json_encode(array('state'=>'0','auction_id'=>$auction_id));
			}else{
				echo json_encode(array('state'=>'1','error'=>'异常处理'));				
			}
			exit;		
		}

		$auction_id = intval($_GET['auction_id']);		$zc_id=$_GET["zc_id"] ? $_GET["zc_id"]:0;
		$params = array();
		$params['id'] = $auction_id;
		//$params['is_fabu'] = '0';
		$model = Model();
		$auction = $model->table('auction')->where($params)->find();
		if(empty($auction)){
			$this->showTip('拍品不存在');
		}
		
		if(($auction['is_fabu']!='0')&&($auction['is_state']!='1')){
			$this->showTip('拍品不存在');
		}
		
		Tpl::output('auction',$auction);		Tpl::output('zc_id',$zc_id);	
		
		$auction_img =  $model->table('auction_img')->where(array('auction_id'=>$auction_id))->order('id asc')->select();
		Tpl::output('auction_img',$auction_img);
		
		$auction_count = $model->table('auction_img')->where(array('auction_id'=>$auction_id))->count();
		Tpl::output('auction_count',$auction_count);
		
		$plat_guize = $model->table('setting')->where(array('name'=>'plat_guize'))->find();
		Tpl::output('plat_guize',$plat_guize);

		Tpl::showpage('publish.step1');
	}
	
	public function step2Op(){		$zc_id=$_GET['zc_id'];
		$auction_id = intval($_GET['auction_id']);
		$model = Model();
		$auction = $model->table('auction')->where(array('id'=>$auction_id))->find();

		if(empty($auction)){
			$this->showTip('操作失败');
		}
		Tpl::output('auction',$auction);
		
		$category0 = $model->table('category')->where(array('id'=>$auction['cate_id']))->find();
		Tpl::output('category0',$category0);
		
		$category1 = $model->table('category')->where(array('id'=>$auction['cate_id_1']))->find();
		Tpl::output('category1',$category1);
		
		$category = $model->table('category')->where(array('parent_id'=>'0'))->order('categorysort asc')->limit('0,8')->select();
		if(!empty($category)){
			foreach($category as $key=>$val){
				$category[$key]['son'] = $model->table('category')->where(array('parent_id'=>$val['id']))->order('categorysort asc')->select();
			}
		}
		Tpl::output('category',$category);

		$setting =  F('bearshopsetting');
		$is_charge = $setting['is_charge'];
		
		Tpl::output('is_charge',$is_charge);		if($zc_id>0){			$zc_times=$model->table('special')->where(array('id'=>$zc_id))->find();			Tpl::output('zc_times',$zc_times);		}		Tpl::output('zc_id',$zc_id);				Tpl::output('time',time());		Tpl::output('time_str',date("Y-m-d H:i",time()));
		Tpl::showpage('publish.step2');				
	}
	
	
	public function step3Op(){
		
		$model = Model();
		$auction = $model->table('auction')->where(array('id'=>intval($_GET['auction_id'])))->find();
		if(empty($auction)){
			$this->showTip('操作失败');
		}
		Tpl::output('auction',$auction);
		Tpl::showpage('publish.step3');
	}
	
	
	public function publishAuctionOp(){
		$auction_video_url=$_POST["auction_video_url"];		
		$auction_id = intval($_POST['auction_id']);		
		$model = Model();
		$auction = $model->table('auction')->where(array('id'=>$auction_id))->find();
		if(empty($auction)){
			echo json_encode(array('state'=>1));
			exit;
		}
		
		$params = array();		
		if(!empty($_POST['freeship'])){
			$params['freeship'] = $_POST['freeship'];
		}
		
		if(!empty($_POST['end_time'])){
			$params['end_time'] = $_POST['end_time'];			
		}
		
		if(!empty($_POST['cate_id'])){
			$params['cate_id'] = $_POST['cate_id'];
			$params['cate_id_1'] = $_POST['cate_id_1'];
		}
		
		if(!empty($_POST['firstprice'])){
			$params['firstprice'] = $_POST['firstprice'];
		}
		
		if(!empty($_POST['addprice'])){
			$params['addprice'] = $_POST['addprice'];
		}
		
		if(!empty($_POST['yikoujia'])){
			$params['yikoujia'] = $_POST['yikoujia'];
		}
		
		if(!empty($_POST['cankaojia'])){
			$params['cankaojia'] = $_POST['cankaojia'];
		}
		
		if(!empty($_POST['cashprice'])){
			$params['cashprice'] = $_POST['cashprice'];
		}else{

			$params['is_state'] = '0';
			$params['is_show'] = '0';			if($_POST["zc_id"]>0){				$zc_times = $model->table('special')->where(array('id'=>$_POST['zc_id']))->find();				$zc_start_time=$zc_times['start_time'];				$params['add_time'] = $zc_start_time;				$params['is_fabu'] = 0;			}			else{				$params['is_fabu'] = 1;				$params['add_time'] = time();			}
			
			$params['cashprice'] = 0;
		}
	$params['auction_video']=$auction_video_url;	$params['Relation_zc_id']=$_POST["zc_id"];	$params['zc_sh']=0;
		
		

		$condition = array();
		$condition['id'] = $auction_id;
		$res = $model->table('auction')->where($condition)->update($params);
		if($res){
			if($params['is_fabu'] == 1){
				$this->tuiinfo($auction_id,$params['end_time']);
			}
			echo json_encode(array('state'=>'1'));			
		}else{
			echo json_encode(array('state'=>'0'));
		}
		exit;		
	}
	

	
	public function delimgOp(){
		$model = Model();
		$img_id = intval($_POST['img_id']);
		$auction_id = intval($_POST['auction_id']);
		
		$auction = $model->table('auction')->where(array('id'=>$auction_id))->find();
		
		$condition = array();
		$condition['id'] = $img_id;
		$condition['auction_id'] = $auction_id;
		
		$auction_img = $model->table('auction_img')->where($condition)->find();
		
		unlink(BASE_UPLOAD_PATH.'/shop/auction/'.$auction['auction_add_date'].'/'.$auction_img['auction_img']);		
		unlink(bearshop_cut_image(BASE_UPLOAD_PATH.'/shop/auction/'.$auction['auction_add_date'].'/'.$val['auction_img']));
		unlink(bearshop_cut_image(BASE_UPLOAD_PATH.'/shop/auction/'.$auction['auction_add_date'].'/'.$val['auction_img'],1));
		$res = $model->table('auction_img')->where($condition)->delete();
		
		if($res){
			echo json_encode(array('state'=>1));
		}else{
			echo json_encode(array('state'=>0));
		}
		exit;
	}
	
	public function chooseendtimeOp(){
		$model = Model();
		$auction = $model->table('auction')->where(array('id'=>intval($_GET['auction_id'])))->find();
		if(empty($auction)){
			$this->showTip('操作失败');
		}
		Tpl::output('auction',$auction);
		Tpl::showpage('publish.choosetime');
	}
}